<?php

namespace App\Admin\Controllers\Aliyun;

use App\Admin\Actions\Aliyun\CreateOssBucket;
use App\Enums\BucketType;
use App\Models\Aliyun;
use App\Models\Application;
use App\Models\ObjectStorageService;
use Dcat\Admin\Admin;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;

class OssController extends AdminController
{
    protected string $title = '存储控制台';

    /**
     * @throws InvalidArgumentException
     */
    public function grid()
    {
        return Grid::make(ObjectStorageService::ofAdmin()->with(['aliyun']), function (Grid $grid) {
            $grid->disableBatchDelete(false);
            $grid->disableEditButton();
            $grid->enableDialogCreate();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (!Admin::user()->isAdministrator() && !$actions->row->is_mine) {
                    $actions->disableEdit();
                }
            });

            $grid->tools([new CreateOssBucket]);

            $grid->quickSearch(['bucket'])
                ->placeholder('搜索存储桶名称');

            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
            }
            $grid->column('aliyun.name', '云账户名称');
            $grid->column('type', '存储类型')
                ->using(BucketType::TYPE_MAP);
            $grid->column('bucket', '桶名称');
            $grid->column('is_mine', '自有')
                ->bool();
            $grid->column('created_at');
        });
    }

    /**
     * Notes   : 获取云账户下的存储桶列表
     *
     * @Date   : 2024/8/1 上午10:13
     * @Author : <Jason.C>
     * @param  Request  $request
     * @return array
     * @throws OssException
     * @throws RequestCore_Exception
     */
    public function buckets(Request $request): array
    {
        $aliyunId = $request->get('q');
        $aliyun = Aliyun::ofAdmin()->find($aliyunId);

        return $aliyun->getOssBuckets();
    }

    /**
     * @throws InvalidArgumentException
     */
    public function form()
    {
        if (Admin::user()->isAdministrator()) {
            $model = ObjectStorageService::class;
        } else {
            $model = ObjectStorageService::ofAdmin()
                ->where('is_mine', true);
        }

        return Form::make($model, function (Form $form) {
            if (Admin::user()->isAdministrator()) {
                $form->select('application_id', '所属应用')
                    ->required()
                    ->options(Application::ofEnabled()->pluck('name', 'id'));
            }

            $form->radio('type', '存储类型')
                ->options(BucketType::TYPE_MAP)
                ->default(BucketType::STORAGE->value)
                ->required();
            $form->select('aliyun_id', '云账户')
                ->required()
                ->options(Aliyun::ofAdmin()->pluck('name', 'id'))
                ->load('bucket', route('admin.oss.buckets'));
            $form->select('bucket', '桶名称')
                ->help('仅能使用标准存储作为存储桶')
                ->required();
        });
    }
}
